home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / dwnodll.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.8 KB  |  351 lines

  1. /* Copyright (C) 1996, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19.  
  20. // $Id: dwnodll.cpp,v 1.2 2000/09/19 19:00:10 lpd Exp $
  21.  
  22. // gsdll class  for MS-Windows
  23.  
  24. // Alternate implementation which doesn't use a DLL at all.
  25. // Instead Ghostscript is statically linked in to the EXE
  26. // and this code makes dw*.c think that it really does have
  27. // the GS DLL loaded.
  28.  
  29.  
  30. #define STRICT
  31. #include <windows.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34.  
  35. extern "C" {
  36. #include "gsdll.h"
  37. #include "gsdllwin.h"
  38. }
  39.  
  40. #include "dwdll.h"   // gsdll_class
  41.  
  42. static char not_loaded[] = "DLL is not loaded";
  43. static char func_null[]  = "A function pointer to the DLL is NULL";
  44. static char not_init[] = "Not initialized";
  45.  
  46. int
  47. gsdll_class::load(const HINSTANCE in_hinstance, 
  48.     const char *name, const long need_version)
  49. {
  50.     // Don't load if already loaded
  51.     if (hmodule)
  52.     return 0;
  53.     hinstance = in_hinstance;
  54.     initialized = FALSE;
  55.   
  56.     // Act like we have loaded the DLL
  57.     // Use the caller instance so that resources are loaded correctly
  58.     hmodule = hinstance;
  59.  
  60. // Need to cause the same thing to happen in gp_mswin.c
  61.     
  62.  
  63.     // Get pointers to functions
  64.     // Under Win32, nothing special is needed.
  65.     // Under Win16, the functions are _export and hence need a
  66.     // thunk created by MakeProcInstance
  67. #ifdef __WIN32__
  68.     c_revision = gsdll_revision;
  69.     c_init = gsdll_init;
  70.     c_execute_begin = gsdll_execute_begin;
  71.     c_execute_cont = gsdll_execute_cont;
  72.     c_execute_end = gsdll_execute_end;
  73.     c_exit = gsdll_exit;
  74.     c_lock_device = gsdll_lock_device;
  75.     c_copy_dib = gsdll_copy_dib;
  76.     c_copy_palette = gsdll_copy_palette;
  77.     c_draw = gsdll_draw;
  78. #else
  79.     c_revision = (PFN_gsdll_revision) MakeProcInstance(gsdll_revision, hmodule);
  80.     if (c_revision == NULL) {
  81.     sprintf(last_error, "Can't make prolog for gsdll_revision\n");
  82.     unload();
  83.     return 1;
  84.     }
  85.     // check DLL version
  86.     c_revision(NULL, NULL, &version, NULL);
  87.     if (version != need_version) {
  88.     sprintf(last_error, "Wrong version of DLL found.\n  Found version %ld\n  Need version  %ld\n", version, need_version);
  89.     unload();
  90.     return 1;
  91.     }
  92.  
  93.     // continue loading other functions */
  94.     c_init = (PFN_gsdll_init) MakeProcInstance(gsdll_init, hmodule);
  95.     if (c_init == NULL) {
  96.     sprintf(last_error, "Can't make prolog for gsdll_init\n");
  97.     unload();
  98.     return 1;
  99.     }
  100.     c_execute_begin = (PFN_gsdll_execute_begin) MakeProcInstance(gsdll_execute_begin, hmodule);
  101.     if (c_execute_begin == NULL) {
  102.     sprintf(last_error, "Can't make prolog for gsdll_execute_begin\n");
  103.     unload();
  104.     return 1;
  105.     }
  106.     c_execute_cont = (PFN_gsdll_execute_cont) MakeProcInstance(gsdll_execute_cont, hmodule);
  107.     if (c_execute_cont == NULL) {
  108.     sprintf(last_error, "Can't make prolog for gsdll_execute_cont\n");
  109.     unload();
  110.     return 1;
  111.     }
  112.     c_execute_end = (PFN_gsdll_execute_end) MakeProcInstance(gsdll_execute_end, hmodule);
  113.     if (c_execute_end == NULL) {
  114.     sprintf(last_error, "Can't make prolog for gsdll_execute_end\n");
  115.     unload();
  116.     return 1;
  117.     }
  118.     c_exit = (PFN_gsdll_exit) MakeProcInstance(gsdll_exit, hmodule);
  119.     if (c_exit == NULL) {
  120.     sprintf(last_error, "Can't make prolog for gsdll_exit\n");
  121.     unload();
  122.     return 1;
  123.     }
  124.     c_lock_device = (PFN_gsdll_lock_device) MakeProcInstance(gsdll_lock_device, hmodule);
  125.     if (c_lock_device == NULL) {
  126.     sprintf(last_error, "Can't make prolog for gsdll_lock_device\n");
  127.     unload();
  128.     return 1;
  129.     }
  130.     c_copy_dib = (PFN_gsdll_copy_dib) MakeProcInstance(gsdll_copy_dib, hmodule);
  131.     if (c_copy_dib == NULL) {
  132.     sprintf(last_error, "Can't make prolog for gsdll_copy_dib\n");
  133.     unload();
  134.     return 1;
  135.     }
  136.     c_copy_palette = (PFN_gsdll_copy_palette) MakeProcInstance(gsdll_copy_palette, hmodule);
  137.     if (c_copy_palette == NULL) {
  138.     sprintf(last_error, "Can't make prolog for gsdll_copy_palette\n");
  139.     unload();
  140.     return 1;
  141.     }
  142.     c_draw = (PFN_gsdll_draw) MakeProcInstance(gsdll_draw, hmodule);
  143.     if (c_draw == NULL) {
  144.     sprintf(last_error, "Can't make prolog for gsdll_draw\n");
  145.     unload();
  146.     return 1;
  147.     }
  148. #endif
  149.     return 0;
  150. }
  151.  
  152. int
  153. gsdll_class::unload(void)
  154. {
  155.     // exit Ghostscript interpreter
  156.     if (initialized) {
  157.     if (!execute_code)
  158.         c_execute_end();
  159.     c_exit();
  160. #ifndef __WIN32__
  161.     FreeProcInstance((FARPROC)callback);
  162. #endif
  163.         callback = NULL;
  164.     }
  165.  
  166.     // For Win16, must free the thunks.
  167. #ifndef __WIN32__
  168. // is it safe to call these with NULL pointers?
  169.     FreeProcInstance(c_revision);
  170.     FreeProcInstance(c_init);
  171.     FreeProcInstance(c_execute_begin);
  172.     FreeProcInstance(c_execute_cont);
  173.     FreeProcInstance(c_execute_end);
  174.     FreeProcInstance(c_exit);
  175.     FreeProcInstance(c_lock_device);
  176.     FreeProcInstance(c_copy_dib);
  177.     FreeProcInstance(c_copy_palette);
  178.     FreeProcInstance(c_draw);
  179. #endif
  180.  
  181.     // Set functions to NULL to prevent use
  182.     c_revision = NULL;
  183.     c_init = NULL;
  184.     c_execute_begin = NULL;
  185.     c_execute_cont = NULL;
  186.     c_execute_end = NULL;
  187.     c_exit = NULL;
  188.     c_lock_device = NULL;
  189.     c_copy_dib = NULL;
  190.     c_copy_palette = NULL;
  191.     c_draw = NULL;
  192.  
  193.     // need to do more than this
  194.     device = NULL;
  195.  
  196.     // Don't do anything else if already unloaded
  197.     if (hmodule == (HINSTANCE)NULL)
  198.     return 0;
  199.  
  200.     return 0;
  201. }
  202.  
  203. int 
  204. gsdll_class::revision(char FAR * FAR *product, char FAR * FAR *copyright, 
  205.     long FAR *revision, long FAR *revisiondate)
  206. {
  207.     if (!hmodule) {
  208.     strcpy(last_error, not_loaded);
  209.     return 1;
  210.     }
  211.     if (!c_revision)
  212.     return c_revision(product, copyright, revision, revisiondate);
  213.     strcpy(last_error, func_null);
  214.     return 1;
  215. }
  216.  
  217. int 
  218. gsdll_class::init(GSDLL_CALLBACK in_callback, HWND hwnd, int argc, char FAR * FAR *argv)
  219. {
  220. int rc;
  221.     execute_code = 0;
  222.     if (!hmodule) {
  223.     strcpy(last_error, not_loaded);
  224.     return 1;
  225.     }
  226.     if (initialized) {
  227.     strcpy(last_error, "Already initialized");
  228.     return 1;
  229.     }
  230.     if (in_callback == (GSDLL_CALLBACK)NULL) {
  231.     strcpy(last_error, "Callback not provided");
  232.     return 1;
  233.     }
  234.  
  235. #ifdef __WIN32__
  236.     callback = in_callback;
  237. #else
  238.     callback = (GSDLL_CALLBACK)MakeProcInstance((FARPROC)in_callback, hinstance);
  239. #endif
  240.  
  241.     if (c_init && c_execute_begin) {
  242.     rc = c_init(callback, hwnd, argc, argv);
  243.     if (rc) {
  244.         sprintf(last_error, "gsdll_init returns %d\nDLL already in use", rc);
  245.         return rc;
  246.     }
  247.     else {
  248.         if ((rc = c_execute_begin()) != 0)
  249.             sprintf(last_error, "gsdll_execute_begin returns %d", rc);
  250.         else
  251.             initialized = TRUE;
  252.         return rc;
  253.     }
  254.     }
  255.     strcpy(last_error, func_null);
  256.     return 1;
  257. }
  258.  
  259. int
  260. gsdll_class::restart(int argc, char FAR * FAR *argv)
  261. {
  262.     if (!hmodule) {
  263.     strcpy(last_error, not_loaded);
  264.     return 1;
  265.     }
  266.     if (!initialized) {
  267.     strcpy(last_error, not_init);
  268.     return 1;
  269.     }
  270.     if (c_execute_end && c_exit) {
  271.     if (!execute_code)
  272.         c_execute_end();
  273.     c_exit();
  274. #ifndef __WIN32__
  275.     FreeProcInstance((FARPROC)callback);
  276. #endif
  277.     // ignore return codes since they we may be aborting from an error
  278.     initialized = FALSE;
  279.     return init(callback, hwnd, argc, argv);
  280.     }
  281.  
  282.     strcpy(last_error, func_null);
  283.     return 1;
  284. }
  285.  
  286. int 
  287. gsdll_class::get_last_error(char *str, int len)
  288. {
  289.     strncpy(str, last_error,  
  290.     (len < sizeof(last_error) ? len : sizeof(last_error)));
  291.     return 0;
  292. }
  293.  
  294.  
  295. int 
  296. gsdll_class::execute(const char FAR *str, int len)
  297. {
  298.     if (!hmodule) {
  299.     strcpy(last_error, not_loaded);
  300.     return 1;
  301.     }
  302.     if (!initialized) {
  303.     strcpy(last_error, not_init);
  304.     return 1;
  305.     }
  306.     if (c_execute_cont) {
  307.     execute_code = c_execute_cont(str, len);
  308.     return execute_code;
  309.     }
  310.  
  311.     strcpy(last_error, func_null);
  312.     return 1;
  313. }
  314.  
  315. int 
  316. gsdll_class::draw(const char FAR *device, HDC hdc, int dx, int dy, int wx, int wy, int sx, int sy)
  317. {
  318. RECT source, dest;
  319.     source.left = sx;
  320.     source.right = sx+wx;
  321.     source.top = sy;
  322.     source.bottom = sy+wy;
  323.  
  324.     dest.left = dx;
  325.     dest.right = dx+wx;
  326.     dest.top = dy;
  327.     dest.bottom = dy+wy;
  328.     c_draw((unsigned char FAR *)device, hdc, &dest, &source);
  329.     return 0;
  330. }
  331.  
  332. HPALETTE 
  333. gsdll_class::copy_palette(const char FAR *device)
  334. {
  335.     return c_copy_palette((unsigned char FAR *)device);
  336. }
  337.  
  338.  
  339. HGLOBAL
  340. gsdll_class::copy_dib(const char FAR *device)
  341. {
  342.     return c_copy_dib((unsigned char FAR *)device);
  343. }
  344.  
  345.  
  346. int 
  347. gsdll_class::lock_device(const char FAR *device, int lock)
  348. {
  349.     return c_lock_device((unsigned char FAR *)device, lock);
  350. }
  351.